package com.note.chh.notebook.UI.enjoy.netMusicFragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.NetworkUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Adapter.NewMusicAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.NewMusicModel;
import com.note.chh.notebook.Model.SongListModel;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseFragment;
import com.note.chh.notebook.UI.enjoy.CheckMusicPictureActivity;
import com.note.chh.notebook.UI.enjoy.PlayMusicActivity;
import com.note.chh.notebook.UI.enjoy.newmusic.NewMusicBannerWebActivity;
import com.note.chh.notebook.UI.enjoy.singerlist.SongListActivity;
import com.note.chh.notebook.Uitls.GlideImageLoader;
import com.note.chh.notebook.Uitls.gson.GsonImpl;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * 新歌榜
 **/
public class NewMusicFragment extends BaseFragment {


    @BindView(R.id.mRecycle_new)
    RecyclerView mRecycleNew;
    private BaseQuickAdapter newMusicAdapter;
    private Banner banner;
    private View inflate;
    private List<NewMusicModel.BannerBean> bannerBean;

    public NewMusicFragment() {
        // Required empty public constructor
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_new_music;
    }

    @Override
    public void initView(View view) {

        mRecycleNew.setLayoutManager(new LinearLayoutManager(getActivity()));
        newMusicAdapter = new NewMusicAdapter(R.layout.item_rank_list);
        newMusicAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecycleNew.setAdapter(newMusicAdapter);

        inflate = getActivity().getLayoutInflater().inflate(R.layout.layout_banner, null);
        banner = inflate.findViewById(R.id.banner);

    }

    @Override
    public void initData() {
        getNewMusic();
    }

    @Override
    public void initListener() {
        super.initListener();
        newMusicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!NetworkUtils.isAvailableByPing()) {//判断网络是否可用
                    showToast(Constant.NETWORKNOUSER);
                } else if (NetworkUtils.isMobileData()) {//判断是否是移动数据  是
                    showMessageDialog(position);
                }else{//否
                    goJump(position);
                }
            }
        });

    }
    //跳转处理
    private void goJump(int position){
        NewMusicModel.DataBean dataBean = (NewMusicModel.DataBean) newMusicAdapter.getData().get(position);
        String hash = dataBean.getHash();
        Bundle bundle = new Bundle();
        bundle.putString("hash",hash);
        bundle.putBoolean("netMusic",true);
        bundle.putInt("position",position);
        bundle.putInt("songType",1);
        bundle.putParcelableArrayList("newList", (ArrayList<NewMusicModel.DataBean>) newMusicAdapter.getData());
        startActivity(PlayMusicActivity.class,bundle);
    }
    //提示网络状态
    private void showMessageDialog(final int position) {
        new QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("提示")
                .setMessage("您当前网络不是wifi，是否继续播放音乐")
                .addAction("不了，心疼流量", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("继续，我流量多", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        goJump(position);
                        dialog.dismiss();
                    }
                })
                .show();
    }

    //获取新歌榜单数据
    private void getNewMusic(){

        OkGo.<String>get("http://m.kugou.com/?json=true")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        NewMusicModel newMusicModel = GsonImpl.get().toObject(response.body(), NewMusicModel.class);
                        List<NewMusicModel.DataBean> data = newMusicModel.getData();
                        bannerBean = newMusicModel.getBanner();
                        List<String> bannerImg = new ArrayList<>();
                        for (int i = 0; i < bannerBean.size(); i++) {
                            bannerImg.add(bannerBean.get(i).getImgurl());
                        }
                        setBannerData(bannerImg);
                        newMusicAdapter.addHeaderView(inflate);
                        newMusicAdapter.setNewData(data);
                    }
                });
    }


    //设置banner数据
    private void setBannerData(List<String> images){
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置指示器
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.CubeOut);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (bannerBean!=null) {
                    NewMusicModel.BannerBean bannerBean = NewMusicFragment.this.bannerBean.get(position);
                    Intent intent = new Intent(getActivity(), NewMusicBannerWebActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("url",bannerBean.getExtra().getTourl());
                    bundle.putString("title",bannerBean.getTitle());
                    intent.putExtras(bundle);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.scale_in, R.anim.alpha_out);
                }
            }
        });
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        if (banner!=null) {
            banner.startAutoPlay();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        if (banner!=null) {
            banner.stopAutoPlay();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在主线程activity Destroy之前先中止Glide请求
//        if (Util.isOnMainThread()) {
//            Glide.with(getActivity()).pauseRequests();
//        }
    }
}
